// import MyTabBar from "@/component/TabBar";
import { Button, DatePicker, Input, NavBar, Toast } from 'antd-mobile'
import Icon from '@/component/Icon'
import './index.scss'
import classNames from 'classnames'
import { billListData } from '@/contants'
import { useNavigate } from 'react-router-dom'
import { useState } from 'react'
import dayjs from 'dayjs'
import { useDispatch } from 'react-redux'
import { addBillList } from '@/store/modules/billStore'
export default function New() {
  const [billType, setBillType] = useState('pay')
  const navigate = useNavigate()
  const [useFor, setUseFor] = useState('')
  const [money, setMoney] = useState(0)
  const [dateVisible, setDataVisible] = useState(false)
  const [currentTime, setCurrentTime] = useState(new Date())
  const [activeIcon, setActiveIcon] = useState(false)
  const dateConfirm = (value) => {
        setCurrentTime(value)
        setDataVisible(false)
  }
  const onUseFor = (item) => {
    setUseFor(item.type)
    setActiveIcon(true)
  }
  const dispatch = useDispatch()
  const submit = () => {
    const data = {
      type: billType,
      useFor,
      money:billType === 'pay' ? -money : +money,
      date: currentTime
    }
    dispatch(addBillList(data))
    setMoney(0)
    setActiveIcon(false)
    Toast.show({
      icon: 'success',
      content: '操作成功',
    })
    setTimeout(() => {
      navigate('/')
    }, 1000)
  }
  return (
    <div className="keepAccounts">
      <NavBar className="nav" onBack={() => navigate(-1)}>
        记一笔
      </NavBar>

      <div className="header">
        <div className="kaType">
          <Button
             shape="rounded"
             className={classNames(billType === 'pay' ? 'selected' : '')}
             onClick={() => setBillType('pay')}
          >
            支出
          </Button>
          <Button
            className={classNames(billType === 'income' ? 'selected' : '')}
            shape="rounded"
            onClick={() => setBillType('income')}
          >
            收入
          </Button>
        </div>

        <div className="kaFormWrapper">
          <div className="kaForm">
            <div className="date">
              <Icon type="calendar" className="icon" />
              <span className="text" onClick={() => setDataVisible(true)}>{dayjs(currentTime).format('YYYY-MM-DD')}</span>
              <DatePicker
                className="kaDate"
                title="记账日期"
                max={new Date()}
                visible={dateVisible}
                onConfirm={dateConfirm}
                onClose={() => setDataVisible(false)
                }
              />
            </div>
            <div className="kaInput">
              <Input
                className="input"
                placeholder="0.00"
                type="number"
                value={money}
                onChange={value => setMoney(value)}
              />
              <span className="iconYuan">¥</span>
            </div>
          </div>
        </div>
      </div>

      <div className="kaTypeList">
        {billListData[billType].map(item => {
          return (
            <div className="kaType" key={item.type}>
              <div className="title">{item.name}</div>
              <div className="list">
                {item.list.map(item => {
                  return (
                    <div
                      className={classNames(
                        'item',
                         useFor === item.type && activeIcon ? 'selected' : ''
                      )}
                      key={item.type}
                      onClick={() => onUseFor(item)}
                    >
                      <div className="icon">
                        <Icon type={item.type} />
                      </div>
                      <div className="text">{item.name}</div>
                    </div>
                  )
                })}
              </div>
            </div>
          )
        })}
      </div>

      <div className="btns">
        <Button className="btn save" onClick={submit}>
          保 存
        </Button>
      </div>
    </div>
  )
}
